package com.mz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.common.dto.GlobalException;
import com.mz.common.dto.KeyValueVo;
import com.mz.common.dto.PaginationVo;
import com.mz.dao.OmsGoodsDetailImgDao;
import com.mz.dao.OmsGoodsInfoDao;
import com.mz.dao.OmsGoodsSpecsDao;
import com.mz.entity.OmsGoodsDetailImg;
import com.mz.entity.OmsGoodsInfo;
import com.mz.entity.OmsGoodsSpecs;
import com.mz.entity.vo.*;
import com.mz.service.OmsGoodsInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * (OmsGoodsInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-12-13 13:38:29
 */
@Service
public class OmsGoodsInfoServiceImpl implements OmsGoodsInfoService {

    @Resource
    private OmsGoodsInfoDao omsGoodsInfoDao;
    @Resource
    private OmsGoodsSpecsDao omsGoodsSpecsDao;
    @Resource
    private OmsGoodsDetailImgDao omsGoodsDetailImgDao;

    @Override
    public IPage getGoodsPagination(PaginationVo paginationVo, GoodsQuery goodsVo) throws Exception {

        IPage page = new Page(paginationVo.getPageIndex(),  paginationVo.getPageSize());
        LambdaQueryWrapper<OmsGoodsInfo> lqs = new LambdaQueryWrapper<>();
        lqs.orderByDesc( OmsGoodsInfo::getCreateTime );
        lqs.eq( goodsVo.getCategoryId()!=null , OmsGoodsInfo::getCategoryId, goodsVo.getCategoryId());
        omsGoodsInfoDao.selectPage(page,lqs );

        return page;
    }

    @Override
    public List getGoodsListByIds(Collection<Long> goodsIdList) throws Exception {

        List<OmsGoodsInfo> omsGoodsInfos = omsGoodsInfoDao.selectBatchIds(goodsIdList);
        return omsGoodsInfos;
    }


    @Override
    @Transactional
    public Long addGoodsInfo(GoodsVo goodsVo) throws Exception {

        //1、创建商品基本信息，并将 "商品id" 返回。
        OmsGoodsInfo omsGoodsInfo = new OmsGoodsInfo();
        BeanUtils.copyProperties( goodsVo, omsGoodsInfo );
        System.out.println(omsGoodsInfo);
        omsGoodsInfoDao.insert(omsGoodsInfo);

        //2、将商品的参数信息、规格信息存入到商品规格表中
        if( goodsVo.getParamList() != null && goodsVo.getParamList().size() != 0){
            for( KeyValueVo keyValueVo :  goodsVo.getParamList() ){
                OmsGoodsSpecs omsGoodsSpecs = new OmsGoodsSpecs();
                omsGoodsSpecs.setGoodsId(omsGoodsInfo.getId())
                        .setType(1)
                        .setHasImg(0)
                        .setGoodsKey(keyValueVo.getKey())
                        .setGoodsValue(keyValueVo.getValue());
                omsGoodsSpecsDao.insert(omsGoodsSpecs);
            }
        }
        if( goodsVo.getSpecList() != null && goodsVo.getSpecList().size() != 0 ){
            for( KeyValueVo keyValueVo : goodsVo.getSpecList() ){
                List<KeyValueVo> valueList = keyValueVo.getValueList();
                for( KeyValueVo value : valueList ){
                    OmsGoodsSpecs omsGoodsSpecs = new OmsGoodsSpecs();
                    omsGoodsSpecs.setGoodsId(omsGoodsInfo.getId())
                            .setType(1)
                            .setHasImg(0)
                            .setGoodsKey(keyValueVo.getKey())
                            .setGoodsValue(value.getValue());
                    omsGoodsSpecsDao.insert(omsGoodsSpecs);
                }
            }
        }
        return omsGoodsInfo.getId();
    }

    @Override
    @Transactional
    public void updateGoodsProductImgVideoList(GoodsProductImgVo goodsProductImgVo) throws Exception {

        //1.将商品的 coverUrl 也转化为产品图的第一张。
        List<ProductImgItem> list = new ArrayList<>();
        list.add( new ProductImgItem( goodsProductImgVo.getCoverUrl(), 0, 1 ) );
        list.addAll( goodsProductImgVo.getProductImgList() );

        //1、更新产品图
        OmsGoodsInfo omsGoodsInfo = new OmsGoodsInfo();
        omsGoodsInfo.setId( goodsProductImgVo.getGoodsId() );
        omsGoodsInfo.setCoverUrl(goodsProductImgVo.getCoverUrl());
        omsGoodsInfoDao.updateById(omsGoodsInfo);

        //2、先删除当前商品的其他产品图
        LambdaQueryWrapper<OmsGoodsDetailImg> lqs = new LambdaQueryWrapper<>();
        lqs.eq(OmsGoodsDetailImg::getGoodsId, goodsProductImgVo.getGoodsId());
        omsGoodsDetailImgDao.delete(lqs);

        //3、创建当前商品的产品图
        for( ProductImgItem item : list ){
            OmsGoodsDetailImg omsGoodsDetailImg = new OmsGoodsDetailImg();
            omsGoodsDetailImg.setGoodsId(goodsProductImgVo.getGoodsId())
                    .setType(0)
                    .setSourceType(item.getSourceType())
                    .setUrl(item.getUrl())
                    .setSort(0);
            omsGoodsDetailImgDao.insert(omsGoodsDetailImg);
        }
    }

    @Override
    @Transactional
    public void updateGoodsDetailImgList(GoodsDetailImgVo goodsDetailImgVo) throws Exception {
        List<ProductImgItem> list = goodsDetailImgVo.getDetailImgList();
        if( list == null || list.size() == 0 ){
            throw GlobalException.gen(10022, "必须上传商品详情图");
        }

        //创建当前商品的详情图
        for( ProductImgItem item : list ){
            OmsGoodsDetailImg omsGoodsDetailImg = new OmsGoodsDetailImg();
            omsGoodsDetailImg.setGoodsId(goodsDetailImgVo.getGoodsId())
                    .setType(1)
                    .setSourceType(item.getSourceType())
                    .setUrl(item.getUrl())
                    .setSort(0);
            omsGoodsDetailImgDao.insert(omsGoodsDetailImg);
        }
    }

    @Override
    @Transactional
    public void updateGoodsGrouding(GoodsGroundingVo goodsGroundingVo) throws Exception {

        if( goodsGroundingVo.getDelayTime() == null ){
            goodsGroundingVo.setDelayTime(new Date());
        }

        OmsGoodsInfo goodsInfo = new OmsGoodsInfo();
        goodsInfo.setId(goodsGroundingVo.getGoodsId());
        goodsInfo.setGrounding(goodsGroundingVo.getGrounding());
        goodsInfo.setDelayTime(goodsGroundingVo.getDelayTime());

        omsGoodsInfoDao.updateById(goodsInfo);
    }

    @Override
    public void removeGoods(List<Long> idList) throws Exception {

        if( idList == null || idList.size() == 0 ){
            throw GlobalException.gen(10014, "商品id不存在");
        }
        omsGoodsInfoDao.deleteBatchIds(idList);
        return;
    }

    @Override
    public GoodsDetailVo getGoodsDetail(Long id) throws Exception {

        //获取商品
        OmsGoodsInfo omsGoodsInfo = omsGoodsInfoDao.selectById(id);
        if( omsGoodsInfo == null ){
            throw GlobalException.gen(10031, "商品不存在");
        }

        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        BeanUtils.copyProperties(omsGoodsInfo, goodsDetailVo);

        //获取商品的产品图
        LambdaQueryWrapper<OmsGoodsDetailImg> productImgLqs = new LambdaQueryWrapper<>();
        productImgLqs.eq(OmsGoodsDetailImg::getType, 0);
        productImgLqs.eq(OmsGoodsDetailImg::getGoodsId, omsGoodsInfo.getId());
        List<OmsGoodsDetailImg> omsGoodsProductImgs = omsGoodsDetailImgDao.selectList(productImgLqs);
        goodsDetailVo.setProductImgList(omsGoodsProductImgs);

        //获取商品的详情图
        LambdaQueryWrapper<OmsGoodsDetailImg> detailImgLqs = new LambdaQueryWrapper<>();
        detailImgLqs.eq(OmsGoodsDetailImg::getType, 1);
        detailImgLqs.eq(OmsGoodsDetailImg::getGoodsId, omsGoodsInfo.getId());
        List<OmsGoodsDetailImg> omsGoodsDetailImgs = omsGoodsDetailImgDao.selectList(detailImgLqs);
        goodsDetailVo.setDetailImgList(omsGoodsDetailImgs);

        //获取商品的参数
        LambdaQueryWrapper<OmsGoodsSpecs> paramsLqs = new LambdaQueryWrapper<>();
        paramsLqs.eq(OmsGoodsSpecs::getGoodsId, omsGoodsInfo.getId());
        paramsLqs.eq(OmsGoodsSpecs::getType, 1);
        List<OmsGoodsSpecs> omsGoodsParams = omsGoodsSpecsDao.selectList(paramsLqs);
        goodsDetailVo.setParamsList(omsGoodsParams);

        //获取商品的规格
        LambdaQueryWrapper<OmsGoodsSpecs> specsLqs = new LambdaQueryWrapper<>();
        specsLqs.eq(OmsGoodsSpecs::getGoodsId, omsGoodsInfo.getId());
        specsLqs.eq(OmsGoodsSpecs::getType, 1);
        List<OmsGoodsSpecs> omsGoodsSpecs = omsGoodsSpecsDao.selectList(specsLqs);
        goodsDetailVo.setSpecsList(omsGoodsSpecs);

        //获取商品的评论
        return goodsDetailVo;
    }
}

